import com.troyworks.hsmf.Hsmf;
import com.troyworks.framework.ui.BaseComponent;
import com.troyworks.events.TEventDispatcher;
import com.troyworks.framework.ui.IHaveChildrenComponents;

/**
 * @author Troy Gardner
 * has an optional label textfield
 * this is a way of binding a skin (movie clip) to button behavior
 * by one of two different methods, one is the stacked clips (more appropriate for things
 * that need to be resized with end caps
 *  and timeline, which is great for simple buttons, 
 *  the frames should be [up][over][down][hit....][inactive][enabled...]
 */
class com.troyworks.framework.ui.MCButton extends BaseComponent {
	public static var className:String = "com.troyworks.framework.ui.MCButton";
	public var isReady:Boolean = false;
	private var isEnabled:Boolean = true;
	private var isMouseOver : Boolean;
	public var label:String;
	public var label_txt:TextField;
	// a utility 
	//public var initListeners:Array = new Array();
	//uses frame labels
	public static var TIMELINE_MC_BUTTON:Number = 0;
	public var upFrameLbl:String = "up";
	public var overFrameLbl:String = "over";
	public var downFrameLbl:String = "down";
	public var hitFrameLbl:String = "down";
	public var outFrameLbl:String = "up";
	public var disabledFrameLbl:String = "disabled";
	public var enabledFrameLbl:String = "up";
	
	//uses stacked clips for up, over, down states
	public static var STACKED_MC_BUTTON:Number = 1;
	public var up_mc:MovieClip;
	public var over_mc:MovieClip;
	public var down_mc:MovieClip;
	public var disabled_mc:MovieClip;
	//public var hit_mc:MovieClip;
	public static var EVTD_CLICK:String = "CLICK";
	public static var EVTD_ROLLOUT:String = "ROLLOUT";
	public static var EVTD_ROLLOVER:String = "ROLLOVER";
	public static var EVTD_DOWN:String = "DOWN";
	public static var EVTD_OUT:String = "OUT";
	public var click_evt:String = EVTD_CLICK;

		
	//used for only a single graphic
	public static var SINGLEFRAME_MC_BUTTON:Number = 2;
	 
	//defaults
	public var MC_ButtonStructure:Number = SINGLEFRAME_MC_BUTTON;
	
	/*************************************
	 *  Constructor
	 */
	
	function MCButton(initState : Function) {
		super(initState, _name+":MCButton", false);
		trace(_name+ ". New MC BUTTON");
		owner = IHaveChildrenComponents(_parent);
	//	$tevD.debugTracesOn = true;
	}
	function onLoad():Void{
		isLoaded = true;
		trace(_name+".MCButton.onLoad" );
		if(up_mc != null && over_mc != null && _totalframes ==1){
			trace("MC_ButtonStructure = STACKED_MC_BUTTON" + up_mc + " " + over_mc);
			MC_ButtonStructure = STACKED_MC_BUTTON;
			up_mc._x = 0;
			over_mc._x = 0;
			down_mc._x = 0;
			disabled_mc._x = 0;
			setUIState("UP");
		}else if(_totalframes > 3){
			trace("MC_ButtonStructure = TIMELINE_MC_BUTTON" );
			MC_ButtonStructure = TIMELINE_MC_BUTTON;
			
		}
		trace(_name+".MCButton.onLoad "  + MC_ButtonStructure + " HIGHLIGHT " + _totalframes);

		snapshotDimensions(this);
		var n:String = String(_name);
		//generate the label and the name and the event
		var a:Number = n.indexOf("__");
		var nm:String =null;
		 if(a > -1){
		  	nm=	n.substring(2, n.length);
		  	click_evt = nm;
		 }else {
		 	nm =n;
		 }

		this.label_txt.text = nm;
		//		 label = nm;
		 enabled = this.isEnabled;
		isReady = true;
		owner.onChildClipLoad(this);
	
	//	this.init = true;
	}
	/*
		private function initContent():Void{
		textField_txt = createTextField("label_txt", 0, 0, 0, __width, __height);
		textField_txt.selectable = false;
		textField_txt.wordWrap = false;
		textField_txt.multiline = false;
		textField_txt.embedFonts = true;
		
		textFormat_tft = new TextFormat(_sFont, _nSize, _nColor);
		disableTextFormat_tft = new TextFormat(_sFont, _nSize, 0x666666);
		
		body = this.createEmptyMovieClip("body", -1);
		
		body.beginFill(0x333333, 0);
		body.moveTo(0, 0);
		body.lineTo(__width, 0);
		body.lineTo(__width, __height);
		body.lineTo(0, __height);
		body.lineTo(0, 0);
		body.endFill();
		
		isMouseOver = false;
		enabled = true;
		embedFont = true;
		textFormat = textFormat_tft;
		updateState();
		
		_visible = true;
		initialized = true;	
	}*/

	function onReleaseHandler():Void {
		trace(_name+".MCButton.onReleaseHandler---------------------------");
		setUIState("OVER");
		trace("dispatch click " + click_evt);
		TEventDispatcher.DEBUG_TRACES_ON = true;
		dispatchEvent( {type:click_evt, target:this, index:hsmID, debug:true} );
				TEventDispatcher.DEBUG_TRACES_ON = false;
			
		//Q_dispatch(Hsmf.USER_SIG);
		trace(_name+".MCButton.onReleaseHandler2222222222---------------------------");
	
	}
	function onReleaseOutsideHandler():Void {
		trace(_name+".MCButton.onReleaseOutsideHandler");
		setUIState("OUT");
		dispatchEvent( {type:EVTD_OUT, target:this, index:hsmID} );
	}
	function onPressHandler():Void {
		trace(_name+".MCButton.onPressHandler");	
		setUIState("DOWN");
		dispatchEvent( {type:EVTD_DOWN, target:this, index:hsmID} );
	}
	function onRollOverHandler():Void {
		trace(_name+".MCButton.onRollOverHandler");
		setUIState("OVER");
		dispatchEvent( {type:EVTD_ROLLOVER, target:this, index:hsmID} );
		isMouseOver = true;
	}
	function onRollOutHandler() :Void{
		trace(_name+".MCButton.onRollOutHandler");
		setUIState("OUT");
		dispatchEvent( {type:EVTD_ROLLOUT, target:this, index:hsmID} );
		isMouseOver = false;
	}
	public function set enabled(val:Boolean) :Void{
			var changed:Boolean =  (val != isEnabled);
		isEnabled = (val == null)?false:val;
		trace(_name +".MCButton.setEnabled " + val);
	
		if((isLoaded && !isReady) || (isReady && changed)){
		if (isEnabled ) {
			//trace("enabling");
				setUIState();
		     //enable the events
			onPress = onPressHandler;
		    onRelease = onReleaseHandler;
			onReleaseOutside =onReleaseOutsideHandler;
			onRollOver = onRollOverHandler;
			onRollOut = onRollOutHandler;
			useHandCursor = true;
		} else {
			//trace("disabling");
			setUIState();
		     //disable everything
			delete(onPress);
		    delete(onRelease);
			delete(onReleaseOutside);
			delete(onRollOver);
			delete(onRollOut);
			useHandCursor = false;
		}
		}
	}
	private function setSize(aWidth:Number, aHeight:Number):Void{
		label_txt._width = aWidth;
		label_txt._height = aHeight;
		_width = aWidth;
		_height = aHeight;
	}
	public function setUIState(msg:String) : Void {
		trace(_name+".MCButton.setUIState."+msg + " enabled " + isEnabled + " type " +MC_ButtonStructure);
		if(isEnabled){
			////////////// ENABLED ///////////////////////////////
			if (msg == null || msg == "UP") {
				trace("going to enabled");
				switch(MC_ButtonStructure){
					case STACKED_MC_BUTTON:{
						trace("via stackedbutton");
						up_mc._visible = true;
						over_mc._visible = false;
						down_mc._visible = false;
						disabled_mc._visible = false;
						break;
					}
					case TIMELINE_MC_BUTTON:{
						
						gotoAndPlay(upFrameLbl);
						trace("via timline line to Frame:" + upFrameLbl + " at Frame: " + _currentframe);
						break;
					}
					case SINGLEFRAME_MC_BUTTON:{
						trace("via singleframe");
						_alpha = 100;
						resetSizeAndPosition();
						break;
					}
					default:{
						REQUIRE(false, "ERROR in MCButton.setUIState, invalid MC_ButtonStructure: " + MC_ButtonStructure);
					}
				}
			} else if (msg == "OVER") {
				switch(MC_ButtonStructure){
					case STACKED_MC_BUTTON:{
						up_mc._visible = false;
						over_mc._visible = true;
						down_mc._visible = false;
						disabled_mc._visible = false;
						break;
					}
					case TIMELINE_MC_BUTTON:{
						gotoAndPlay(overFrameLbl);
						break;
					}
					case SINGLEFRAME_MC_BUTTON:{
			//			trace("single frame _________________________________");
			
						this._x = _ox-2;
						this._y = _oy-2;
						this._width = this._owidth + 4;
						this._height =this._oheight + 4;
						break;
					}	
				}
			} else if (msg == "DOWN") {
				switch(MC_ButtonStructure){
					case STACKED_MC_BUTTON:{
						up_mc._visible = false;
						over_mc._visible = false;
						down_mc._visible = true;
						disabled_mc._visible = false;
						break;
					}
					case TIMELINE_MC_BUTTON:{
						gotoAndPlay(downFrameLbl);
						break;
					}
					case SINGLEFRAME_MC_BUTTON:{
						this._x = _ox + 1;
						this._y = _oy + 1;
						break;
					}	
				}
			} else if (msg == "OUT"){
				switch(MC_ButtonStructure){
					case STACKED_MC_BUTTON:{
						up_mc._visible = true;
						over_mc._visible = false;
						down_mc._visible = false;
						disabled_mc._visible = false;
						break;
					}
					case TIMELINE_MC_BUTTON:{
						gotoAndPlay(outFrameLbl);
						break;
					}
					case SINGLEFRAME_MC_BUTTON:{
						resetSizeAndPosition();
						break;
					}	
				}
			}				
		}else{
			///////////////NOT ENABLED ////////////////////////////////////
				switch(MC_ButtonStructure){
					case STACKED_MC_BUTTON:{
				
						if(disabled_mc == null){
							up_mc._visible = true;
							up_mc._alpha = 30;
				
						}else{
							up_mc._visible = false;
							disabled_mc._visible = true;
						}					
						over_mc._visible = false;
						down_mc._visible = false;
						break;
					}
					case TIMELINE_MC_BUTTON:{
					
						gotoAndPlay(disabledFrameLbl);
						break;
					}
					case SINGLEFRAME_MC_BUTTON:{
						_alpha =30;
						break;
					}	
				}
		}
		
	}

}